package org.jackysoft.aop;
import org.apache.commons.lang.ArrayUtils;
import org.jackysoft.entity.HistoryFileItem;
import org.jackysoft.entity.User;
import org.jackysoft.util.FileInfo;
import org.springframework.dao.DataAccessException;
public aspect FileDataEncoder {
    
	public pointcut downloadFile(String id):args(id)&&execution(public FileInfo org.jackysoft.service.DownloadService.download(String) throws DataAccessException);
	public pointcut openHistoryDoc(String id):args(id)&&execution(public  HistoryFileItem org.jackysoft.service.HistoryFileItemService.openHistoryDoc(String) throws DataAccessException);

	public pointcut uploadFile(byte[] files,String uploadFileName, String folderId):args(files,uploadFileName,folderId)
	&&execution(public void org.jackysoft.service.UploadFileService.upload(byte[] ,String, String)throws DataAccessException);
	        
	public pointcut saveToHistory(byte[] fileData, String title, User owner, String originId, String fileType):args(fileData,title,owner,originId,fileType)
	&&execution(public  void org.jackysoft.service.HistoryFileItemService.save(byte[], String , User , String , String) throws DataAccessException);
	
	
	
    //decode
    FileInfo around(String id):downloadFile(id){
    	
    	FileInfo f = proceed(id);
    	ArrayUtils.reverse(f.getData());    	
    	return f;
    };
    
    //decode
    HistoryFileItem around(String id):openHistoryDoc(id){
    	HistoryFileItem f = proceed(id);
    	ArrayUtils.reverse(f.getData());
    	return f;
    }
    
    
    //encode
    void around(byte[] files,String uploadFileName, String folderId):uploadFile(files,uploadFileName,folderId){
    	if(files==null) return;
    	ArrayUtils.reverse(files);       	
       	proceed(files,uploadFileName,folderId);
    }
    void around(byte[] fileData, String title, User owner, String originId, String fileType):saveToHistory(fileData,title,owner,originId,fileType){
    	if(fileData==null)return;
    	ArrayUtils.reverse(fileData);
    	proceed(fileData,title,owner,originId,fileType);
    }
    
    
    
}
